package com.zxl.grocery.microservice.crawler.pipeline;

import com.zxl.grocery.microservice.common.util.ToolUtil;
import com.zxl.grocery.microservice.crawler.entity.CrawlerBeautiful;
import com.zxl.grocery.microservice.crawler.service.biz.CrawlerBeautifulBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Lzx
 * @Description: 美图爬虫
 * @Date: Created in 14:43 2018/1/16
 * @Modified By:
 */
@Repository
public class BeautifulPipeline implements Pipeline {
    @Autowired
    private CrawlerBeautifulBiz crawlerBeautifulBiz;

    @Override
    public void process(ResultItems resultItems, Task task) {
        for (Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {
            if (entry.getKey().equals("beautiful")) {
                CrawlerBeautiful beautiful=(CrawlerBeautiful) entry.getValue();

                CrawlerBeautiful param = new CrawlerBeautiful();
                param.setLink(beautiful.getLink());
                CrawlerBeautiful beautifuls = crawlerBeautifulBiz.selectOne(param);
                if (ToolUtil.isEmpty(beautifuls)) {//检查链接是否已存在
                    beautiful.setTypeId(1);
                    beautiful.setAuthor("zxl");
                    beautiful.setCreateDate(new Date());
                    beautiful.setStatus(1);
                    beautiful.setPlacedTop(false);
                    beautiful.setRecommend(false);
                    beautiful.setModifyDate(new Date());
                    crawlerBeautifulBiz.insertSelective(beautiful);
                }
            }

        }
    }
}
